/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;


import entity.AddressEntity;
import entity.BranchEntity;
//import entity.FnBBranchEntity;
//import entity.FnBIOTREntity;
import entity.IndustryTypeEntity;
//import entity.PackagePurchasedEntity;
//import entity.ServicePackageEntity;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
//import util.exception.ChangeServicePackageException;
//import util.exception.StakeholderBizRegNumberExist;
//import util.exception.StakeholderUsernameExist;
import java.util.List;
import entity.StakeholderEntity;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.SelectEvent;
//import session.stateful.BranchSessionBean;
//import session.stateful.ServiceSessionBean;
import java.security.SecureRandom;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import session.stateless.BranchSessionBean;
import session.stateless.StakeholderSessionBean;
import util.exception.ExistException;

/**
 *
 * @author jamie
 */

@ManagedBean
@SessionScoped 
public class AddStakeholderManagedBean implements Serializable{

    @EJB
    private StakeholderSessionBean stakeholderSessionBean;
    @EJB
    private BranchSessionBean branchSessionBean;
    //@EJB
    //private ServiceSessionBean serviceSessionBean;
    private String statusMessage;
    private Date dateExpired;
    private String oldPassword;
    private String newPassword;
    private String retypeNewPassword;
    private int numOfMonth;
    private int numOfMonthExtend;
    private AddressEntity stakeholderAddress = new AddressEntity();
    private AddressEntity branchAddress = new AddressEntity();
    private StakeholderEntity selectedStakeholder;
    private StakeholderEntity stakeholder = new StakeholderEntity();
   // private FnBBranchEntity selectedBranch = new FnBBranchEntity();
    private IndustryTypeEntity industryType;
   // private ServicePackageEntity selectedServicePackage;
   // private PackagePurchasedEntity currentServicePackage;
    private List<IndustryTypeEntity> industryTypeList;
   // private List<FnBBranchEntity> fnBBranchList = new ArrayList<FnBBranchEntity>();
    private List<BranchEntity> branchList;
   // private FnBIOTREntity fnBIOTR = new FnBIOTREntity();
    private SecureRandom random = new SecureRandom();
    
    public AddStakeholderManagedBean(){
    
    }

     public void register(ActionEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        if (getStakeholder().getCorpEmail().indexOf("@") == -1) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Email address must contains @", ""));
            return;

        }

        try {
            String generatedPassword = randomString();
            String encryptedPassword = encrypt(generatedPassword);
            getStakeholder().setPassword(encryptedPassword);
            getStakeholderSessionBean().addNewStakeholder(getStakeholder(), getStakeholderAddress(), getIndustryType());
          
            setStatusMessage("New stakeholder added successfully");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_INFO, "Add New Stakeholder Result: " + getStatusMessage(), ""));
        } catch (ExistException ex) {
            setStatusMessage(ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Add New Stakeholder Result: " + getStatusMessage(), ""));
        }
    }
     
  public String randomString(){
        String generatedString= new BigInteger(100, random).toString(32);
        return generatedString;
    }
    
    public String encrypt(String inputString) throws UnsupportedEncodingException, NoSuchAlgorithmException{
        String outputString= util.encryption.PasswordService.getInstance().encrypt(inputString);
        return outputString;
    }

    public boolean validateSamePassword() {
        if (retypeNewPassword.equals(newPassword))
            return true;
        else 
            return false;
    }
    
    
     public void initIndustryType(PhaseEvent event) {
        System.out.println(stakeholderSessionBean == null);
       industryTypeList = stakeholderSessionBean.getIndustryTypeList();
    }
     
        public void retrieveStakeholderInfo() {
        this.setSelectedStakeholder(stakeholderSessionBean.retrieveStakeholderInfoByUsername(stakeholder.getUsername()));
    }

    public List<StakeholderEntity> getStakeholders() {
        return getStakeholderSessionBean().getAllStakeholders();
    }
    
    /**
     * @return the stakeholderSessionBean
     */
    public StakeholderSessionBean getStakeholderSessionBean() {
        return stakeholderSessionBean;
    }

    /**
     * @return the branchSessionBean
     */
    public BranchSessionBean getBranchSessionBean() {
        return branchSessionBean;
    }

    /**
     * @return the statusMessage
     */
    public String getStatusMessage() {
        return statusMessage;
    }

    /**
     * @return the dateExpired
     */
    public Date getDateExpired() {
        return dateExpired;
    }

    /**
     * @return the oldPassword
     */
    public String getOldPassword() {
        return oldPassword;
    }

    /**
     * @return the newPassword
     */
    public String getNewPassword() {
        return newPassword;
    }

    /**
     * @return the retypeNewPassword
     */
    public String getRetypeNewPassword() {
        return retypeNewPassword;
    }

    /**
     * @return the numOfMonth
     */
    public int getNumOfMonth() {
        return numOfMonth;
    }

    /**
     * @return the numOfMonthExtend
     */
    public int getNumOfMonthExtend() {
        return numOfMonthExtend;
    }

    /**
     * @return the stakeholderAddress
     */
    public AddressEntity getStakeholderAddress() {
        return stakeholderAddress;
    }

    /**
     * @return the branchAddress
     */
    public AddressEntity getBranchAddress() {
        return branchAddress;
    }

    /**
     * @return the selectedStakeholder
     */
    public StakeholderEntity getSelectedStakeholder() {
        return selectedStakeholder;
    }

    /**
     * @return the stakeholder
     */
    public StakeholderEntity getStakeholder() {
        return stakeholder;
    }

    /**
     * @return the industryType
     */
    public IndustryTypeEntity getIndustryType() {
        return industryType;
    }

    /**
     * @return the industryTypeList
     */
    public List<IndustryTypeEntity> getIndustryTypeList() {
        return industryTypeList;
    }

    /**
     * @return the branchList
     */
    public List<BranchEntity> getBranchList() {
        return branchList;
    }

    /**
     * @return the random
     */
    public SecureRandom getRandom() {
        return random;
    }

    /**
     * @param stakeholderSessionBean the stakeholderSessionBean to set
     */
    public void setStakeholderSessionBean(StakeholderSessionBean stakeholderSessionBean) {
        this.stakeholderSessionBean = stakeholderSessionBean;
    }

    /**
     * @param branchSessionBean the branchSessionBean to set
     */
    public void setBranchSessionBean(BranchSessionBean branchSessionBean) {
        this.branchSessionBean = branchSessionBean;
    }

    /**
     * @param statusMessage the statusMessage to set
     */
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    /**
     * @param dateExpired the dateExpired to set
     */
    public void setDateExpired(Date dateExpired) {
        this.dateExpired = dateExpired;
    }

    /**
     * @param oldPassword the oldPassword to set
     */
    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    /**
     * @param newPassword the newPassword to set
     */
    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    /**
     * @param retypeNewPassword the retypeNewPassword to set
     */
    public void setRetypeNewPassword(String retypeNewPassword) {
        this.retypeNewPassword = retypeNewPassword;
    }

    /**
     * @param numOfMonth the numOfMonth to set
     */
    public void setNumOfMonth(int numOfMonth) {
        this.numOfMonth = numOfMonth;
    }

    /**
     * @param numOfMonthExtend the numOfMonthExtend to set
     */
    public void setNumOfMonthExtend(int numOfMonthExtend) {
        this.numOfMonthExtend = numOfMonthExtend;
    }

    /**
     * @param stakeholderAddress the stakeholderAddress to set
     */
    public void setStakeholderAddress(AddressEntity stakeholderAddress) {
        this.stakeholderAddress = stakeholderAddress;
    }

    /**
     * @param branchAddress the branchAddress to set
     */
    public void setBranchAddress(AddressEntity branchAddress) {
        this.branchAddress = branchAddress;
    }

    /**
     * @param selectedStakeholder the selectedStakeholder to set
     */
    public void setSelectedStakeholder(StakeholderEntity selectedStakeholder) {
        this.selectedStakeholder = selectedStakeholder;
    }

    /**
     * @param stakeholder the stakeholder to set
     */
    public void setStakeholder(StakeholderEntity stakeholder) {
        this.stakeholder = stakeholder;
    }

    /**
     * @param industryType the industryType to set
     */
    public void setIndustryType(IndustryTypeEntity industryType) {
        this.industryType = industryType;
    }

    /**
     * @param industryTypeList the industryTypeList to set
     */
    public void setIndustryTypeList(List<IndustryTypeEntity> industryTypeList) {
        this.industryTypeList = industryTypeList;
    }

    /**
     * @param branchList the branchList to set
     */
    public void setBranchList(List<BranchEntity> branchList) {
        this.branchList = branchList;
    }

    /**
     * @param random the random to set
     */
    public void setRandom(SecureRandom random) {
        this.random = random;
    }
    
}
